home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 050 / pspaddr3.arc / PSPADDR3.C next >
Encoding:
C/C++ Source or Header  |  1987-05-29  |  4.9 KB  |  150 lines

  1. /* This program 
  2.     - prints the addresses of all the PSP's currently being used in memory and
  3.         the handles opened in each of those processes.
  4.         Handy if you have a lot of parent and child processes loaded.
  5.     - was compiled using Lattice 3.00h
  6.     - ASSUMES small memory model.  I was too lazy to do a segread() to get the
  7.         ds register.  Instead, I took Lattice's value in _SS since ds == ss.
  8.     - ASSUMES your environment area does not exceed 1000 bytes.  If it does you
  9.         need to change the array, environment, and the value in movedata().
  10.         DOS has a theoretical maximum of 32K for its environment.
  11.         - chgd for DOS 3.3, ps.environ is not null in command.com, instead after
  12.         the double null is the name of the command processor i.e. COMMAND.COM
  13.   */
  14.  
  15. struct PROG_S_PRE {                /* Program Segment Prefix */
  16.  unsigned int interrpt;    /* first two bytes are the instruction int 20h */
  17.                     /* this is a carryover from CP/M when you could */
  18.                     /* jump to the first byte of your program to exit */
  19.  unsigned int machine;    /* Memory size in paragraphs (16-bytes blocks) */
  20.  char dos1;
  21.  char call_dis[5];        /* FAR CALL to MS-DOS function dispatcher */
  22.  unsigned long Int22;    /* previous INT 22h, terminate process address IP,CS */
  23.  unsigned long Int23;    /* previous INT 23h, CNTRL-C exit address IP,CS */
  24.  unsigned long Int24;    /* previous INT 24h, Critical Error address IP,CS */
  25.  unsigned int pre_psp;    /* previous PSP segment */
  26.  unsigned char hndl[20];    /* file handles - FF means the handle not assigned */
  27.  unsigned int environ;    /* segment address of the environment block */
  28.  char dos2[4];
  29.  unsigned int num_hdl;    /* number of dos handles allocated */
  30.  unsigned int hndl_off;    /* offset  of where the file handles are located */
  31.  unsigned int hndl_seg;    /* segment of where the file handles are located */ 
  32.  char dos3[24];
  33.  char dispatch[12];        /* code to call MS-DOS dispatcher INT 21 instructions */
  34.  char fcb1[16];        /* unopened File Control Block #1 */
  35.  char fcb2[16];        /* unopened File Control Block #2 */
  36.  char dos4[4];
  37.  char dta[128];        /* default Disk Transfer Area */
  38.  } ps;
  39.  
  40.  struct EXTENDED_UNOPENED_FCB {
  41.      char extd;
  42.      char res[5];
  43.      char attr;
  44.      char drive;
  45.      char file[8];
  46.      char ext[3];
  47.      unsigned int cur_blk;
  48.      unsigned int rec_sz;
  49.      unsigned long file_sz;
  50.      unsigned int date;
  51.      unsigned int time;
  52.      char res2[8];
  53.      unsigned char cur_rec;
  54.      unsigned long rel_rec;
  55.      } exfcb;
  56.  
  57.      
  58.  
  59. extern int _psp[];
  60. extern int _env;
  61. extern int _esize;
  62. extern int _ss;
  63. char environment[1000];
  64. char command_com[] = "COMMAND.COM";
  65. char *double_null();
  66.  
  67. void main()
  68. {
  69.  
  70.     unsigned int psp;
  71.     char *p;
  72.     int cmd_com=0;
  73.     int first_time = 1;
  74.  
  75.  
  76.     if( *(long *)(_env+_esize-2) != 0x00010000 )
  77.     {
  78.         cprintf("Your operating system does not have the name of your\n");
  79.         cprintf("program at the end of the environment.  You need DOS 3.1 or\n");
  80.         cprintf("above.  Or you are running Novell Advance Netware ver 1.02\n");
  81.         cprintf("which clobbers the name area.\n");
  82.         _exit(1);
  83.     }
  84.     psp = _psp[1];        /* get the segment word not the offset word */
  85.  
  86. /* get current volume name and put in dta */
  87.      exfcb.extd = 0xff;    /* makes it an extended fcb */
  88.      exfcb.attr = 0x08;    /* asking for volume */
  89.      exfcb.drive = 0;    /* of current drive  */
  90.      strcpy( exfcb.file, "???????????");    /* both fields, file & ext */
  91.     rstdta();                /* reset DTA to be pointing in the PSP */
  92.     bdos( 0x11, &exfcb, 0 );
  93.     
  94.     do
  95.     {
  96. /* move chucks of memory into our data segment area so that we can work with it */
  97.         movedata( psp, 0, _ss, &ps, sizeof( struct PROG_S_PRE) );
  98.         movedata( ps.environ, 0, _ss, &environment, 1000 );
  99.  
  100. /* easy way to get progname for current program */
  101.         if( first_time )
  102.         {
  103.             cprintf("I'm %s with machine memory of %dK\n",
  104.                 ( _env + _esize +2 ), ps.machine >> 6 );
  105.             if( ps.dta[0] == '\377' )
  106.             {
  107.                 ps.dta[19] = '\0';
  108.                 cprintf("   current volume name is %s\n", &ps.dta[8]);
  109.             }
  110.             cprintf("\n");
  111.             first_time = 0;
  112.         }
  113.  
  114.         if( ps.pre_psp == ps.hndl_seg ) cmd_com = 1;    /* in COMMAND.COM */
  115.         if( ps.environ == 0 )        /* not DOS 3.3 */
  116.             p = command_com;
  117.         else {        
  118.         
  119. /* find the name of the program following the environment space, Page 4-3 of
  120.     MS-DOS manual, document # 8411-310-02, part # 036-014-012 */
  121.             p = environment;
  122.             while( *double_null( &p ));
  123.  
  124.                if( cmd_com ) p++;  else  p += 3;
  125.         }
  126.         cprintf("psp at %dK for %s", psp>>6, p );
  127.         cprintf(" maximum number of handles is %d\n",ps.num_hdl);
  128.         if( ps.hndl_off == 0x18 && ps.hndl_seg == psp )
  129.         { 
  130. /* display which handles are active for each program */
  131.         /* the field psp is being used as a work int - not good form but ...*/
  132.             for( psp = 0; psp < 20; psp++)
  133.             {
  134.                 if( ps.hndl[psp] != 0xff )
  135.                     cprintf(" %d",psp);
  136.             }
  137.             cprintf("\n");
  138.             psp = ps.pre_psp;
  139.         } else {
  140.             cprintf("handles not in psp ");
  141.         }
  142.     } while( ! cmd_com );
  143. }
  144. char *double_null( p )
  145. char **p;
  146. {
  147.     while( *(*p)++ );
  148.     return( *p );
  149. }
  150.